var app = getApp()
var server = require('../../../utils/util');
Page({
  data:{
    current: 0,
    tot:0,
    Ids:[],
    listgoods:[],  
  swiper:{
      indicatorDots: false,
      autoplay: false,
      interval: 5000,
      duration: 1000
      }
  },
  onPullDownRefresh: function () {
    console.log('onPullDownRefresh')
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    // 页面初始化 options为页面跳转所带来的参数
    var  id=options.id;
     var that = this;
     wx.getStorage({
       key: 'userId',
       success: function(res){
          console.log("查询参数。。。。。。。。"+res);
          console.log(res);
      wx.request({
        url: app.getUrl()+"/dd_userList.do?userId="+res.data,
        method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
       header: {
        "content-type": "application/json"
      },
        success: function(a){
          // success
            var da = a.data.newgoods;
           console.log('===========返回参数=================');
             console.log(a);
        that.setData({"listgoods":a.data.dds});
        },
        fail: function(res) {
          // fail
        },
        complete: function(res) {
          // complete
        }
      })
       },
       fail: function(res) {
          console.log('===========查询缓存失败=================');
          wx.redirectTo({
               url:"/pages/shop/login/login"
             })
       },
       complete: function(res) {
         // complete
       }
     })
       
    //ajax请求数据
    // wx.request({
    //         url: 'http://www.htmlk.cn/json-test/lists.json',
    //         header: {
    //             'Content-Type': 'application/json'
    //         },
    //         success: function(res) {
    //            switch1(res.data);
    //            console.log(res.data);
    //            that.setData({
    //                listgoods:res.data
    //            });
    //         }
    //     })
    //对商品进行价格排序
    // console.log(this.data.listgoods);
    // switch1(this.data.listgoods);
    // function switch1(odata){
    //     for(var i=0;i<odata.length-1;i++){
    //              //console.log(odata[i].price);
    //             for(var j=0;j<odata.length-i-1;j++){
    //                    // console.log(parseInt(odata[j].price)+"-----"+parseInt(odata[j+1].price));
    //               if(parseInt(odata[j].price)>parseInt(odata[j+1].price)){
    //                 var temp=odata[j];
    //                     odata[j]=odata[j+1];
    //                     odata[j+1]=temp;
    //               }
    //             }
    //     }
    // }    
  },
  //详情页跳转
  lookdetail:function(e){
    var lookid=e.currentTarget.dataset;
    console.log(e.currentTarget.dataset.id);
    wx.navigateTo({
      url:"/pages/yiguo/detail/detail?id="+lookid.id
    })
  },
  totalCar:function(e){
        var tot = this.data.tot;
        var id = e.currentTarget.dataset.id;
        var price = e.currentTarget.dataset.price*1;
        var ids = this.data.Ids;
        console.log("选中ids。。。。。。。。");
        console.log(ids);
        if(server.contains(ids,id)){
           console.log("包含 详见。。。。。。。。");
             for(var i=0; i<ids.length; i++) {
                if(ids[i] == id) {
                   ids.splice(i, 1);
                    break;
                }
              }
            this.setData({"Ids":ids});
             this.setData({"tot":tot-price});
            console.log("=========="+ids);
        }else{
           ids.push(id);
           this.setData({"Ids":ids});
           console.log("选中ids==============。");
           console.log(this.data.Ids);
           this.setData({"tot":tot+price});
        }
         
  },
  tj:function(){
     wx.navigateTo({
      url:"/pages/yiguo/pay/pay"
    })
  },
  switchSlider:function(e){
    this.setData({
      current:e.target.dataset.index
    })
  },
  changeSlider:function(e){
    this.setData({
      current: e.detail.current
    })
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    var that = this;
     wx.getStorage({
       key: 'userId',
       success: function(res){
          console.log("查询参数。。。。。。。。"+res);
          console.log(res);
      wx.request({
        url: app.getUrl()+"/dd_userList.do?userId="+res.data,
        method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
       header: {
        "content-type": "application/json"
      },
        success: function(a){
          // success
            var da = a.data.newgoods;
           console.log('===========返回参数=================');
             console.log(a);
        that.setData({"listgoods":a.data.dds});
        },
        fail: function(res) {
          // fail
        },
        complete: function(res) {
          // complete
        }
      })
       },
       fail: function(res) {
          console.log('===========查询缓存失败=================');
          wx.redirectTo({
               url:"/pages/shop/login/login"
             })
       },
       complete: function(res) {
         // complete
       }
     })
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  sc:function(e){
       var that = this;
        var id = e.currentTarget.dataset.id;
    var lookid=e.currentTarget.dataset;
  console.log("当前。。。。。。。。");
    console.log(e.currentTarget.dataset);
      wx.request({
        url: app.getUrl()+"/dd_delete2.do?id="+lookid.id,
        method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
       header: {
        "content-type": "application/json"
      },
        success: function(a){
          wx.getStorage({
       key: 'userId',
       success: function(res){
          console.log("查询参数。。。。。。。。"+res);
          console.log(res);
      wx.request({
        url: app.getUrl()+"/dd_userList.do?userId="+res.data,
        method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
       header: {
        "content-type": "application/json"
      },
        success: function(a){
          // success
            var da = a.data.newgoods;
           console.log('===========返回参数=================');
             console.log(a);
        that.setData({"listgoods":a.data.dds});
        },
        fail: function(res) {
          // fail
        },
        complete: function(res) {
          // complete
        }
      })
       },
       fail: function(res) {
          console.log('===========查询缓存失败=================');
          wx.redirectTo({
               url:"/pages/shop/login/login"
             })
       },
       complete: function(res) {
         // complete
       }
     })

        },
        fail: function(res) {
          // fail
        },
        complete: function(res) {
          // complete
        }
      })
  }

})
